package com.capgemini.diabeo.dummy;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.capgemini.diabeo.AlimentsEntry;
import com.capgemini.diabeo.DbOpenHelper;

/**
 * Helper class for providing sample content for user interfaces created by
 * Android template wizards.
 * <p>
 * TODO: Replace all uses of this class before publishing your app.
 */
public class DummyContent {
	
	public static DbOpenHelper dbOpenHelper;
	static String entree1;
	/**
	 * An array of sample (dummy) items.
	 */
	public static List<DishesItem> ITEMS = new ArrayList<DishesItem>();

	/**
	 * A map of sample (dummy) items, by ID.
	 */
	public static Map<String, DishesItem> ITEM_MAP = new HashMap<String, DishesItem>();

	public static void setContext(Context c) {
		
        dbOpenHelper = new DbOpenHelper(c);
        try {
			dbOpenHelper.createDataBase();
		} catch (IOException e) {
			throw new Error("Unable to create database");
		}
        try {
        	 
        	dbOpenHelper.openDataBase();
     
     	}catch(SQLException sqle){
     
     		throw sqle;
     
     	}
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
	    if (db.isOpen()) {
	    	Cursor cursor = readCategories();
	        if (cursor.moveToFirst()) {
	            do {
	            	int itemId = cursor.getPosition() + 1;
	            	DishesItem item = new DishesItem(Integer.toString(itemId), cursor.getString(cursor.getColumnIndex(AlimentsEntry.COLUMN_NAME_CATEGORIE)));
	                addItem(item);
	            } while (cursor.moveToNext());
	        }
	    }
	}

	private static void addItem(DishesItem item) {
		ITEMS.add(item);
		ITEM_MAP.put(item.id, item);
	}

	/**
	 * A dummy item representing a piece of content.
	 */
	public static class DishesItem {
		public String id;
		public String content;

		public DishesItem(String id, String content) {
			this.id = id;
			this.content = content;
		}

		@Override
		public String toString() {
			return content;
		}
	}
	
	 public static Cursor readCategories()
	    {
	    	 
		  SQLiteDatabase dbread = dbOpenHelper.getReadableDatabase();
		  String[] projection = {
		    		 AlimentsEntry.COLUMN_NAME_CATEGORIE };
		     /*String sortOrder = AlimentsEntry.COLUMN_NAME_ALIMENT_ID + " DESC";*/

		     Cursor c = dbread.query(
		    		 AlimentsEntry.TABLE_NAME,
		    		 projection,
		         null,
		         null,
		         AlimentsEntry.COLUMN_NAME_CATEGORIE,
		         null,
		         null
		         );

	    return c;    
	    }
}
